cmake_minimum_required(VERSION 2.8.12)

set(progname random_util)

include(CheckIncludeFiles)
include(CheckFunctionExists)
include(CheckPrototypeDefinition)

CHECK_INCLUDE_FILES(getopt.h HAVE_GETOPT_H)
CHECK_INCLUDE_FILES(unistd.h HAVE_UNISTD_H)

if (HAVE_GETOPT_H)
CHECK_PROTOTYPE_DEFINITION(
	getopt_long 
	"int getopt_long(int nargc, char * const *nargv, const char *options, const struct option *long_options, int *idx)"
	-1
	"getopt.h"
	HAVE_GETOPT_PROTOTYPE_GETOPT)
endif()
if (HAVE_UNISTD_H)
CHECK_PROTOTYPE_DEFINITION(
	getopt_long 
	"int getopt_long(int nargc, char * const *nargv, const char *options, const struct option *long_options, int *idx)"
	-1
	"unistd.h"
	HAVE_GETOPT_PROTOTYPE_UNISTD)
endif()
CHECK_FUNCTION_EXISTS(getopt_long HAVE_GETOPT_LONG_FUNCTION)

set(PROJ_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/include")

if (MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

aux_source_directory(${CMAKE_SOURCE_DIR}/src PROJ_SOURCE_FILES)

if (NOT HAVE_GETOPT_PROTOTYPE_GETOPT AND NOT HAVE_GETOPT_PROTOTYPE_UNISTD)
set(HAVE_GETOPT_H "1")
list(APPEND PROJ_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/win_getopt")
endif()

if(NOT HAVE_GETOPT_LONG_FUNCTION)
list(APPEND PROJ_SOURCE_FILES ${CMAKE_SOURCE_DIR}/win_getopt/getopt.c)
endif()

if(HAVE_GETOPT_H)
add_definitions(-DHAVE_GETOPT_H)
endif()
if(HAVE_UNISTD_H)
add_definitions(-DHAVE_UNISTD_H)
endif()

include_directories(
	${PROJ_INCLUDE_DIRS}
)

add_executable(${progname} ${PROJ_SOURCE_FILES})
target_link_libraries(${progname}
	${PROJ_LIBRARIES}
)